/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/
#pragma once

#include <QtCore/QObject>

#include "core/server.h"

#include "clientcoredll.h"

namespace greenhills {

namespace net {
    class CMsgClientSocketConnection;
}

namespace event {
    class CEvent;
    class CActionEvent;
    class CSynchronousChannel;
    class CAsynchronousChannel;
}

namespace client {
namespace core {

/** @brief Greenhills server proxy object
 *
 * This class represents proxy object of greenhills server. All methods called
 * on this object are serialized and sent to greenhills server.
 */
class GH_CLIENTCORE_QT_CLASS CProxy
    : public QObject, public greenhills::core::CServer
{
    Q_OBJECT;

public:
    static CProxy *instance();

private:
    CProxy(QObject *parent);

public:
    bool connect(const std::string &pHost);
    void disconnect();
    bool isConnected();

public: // IServer
    virtual greenhills::core::CClientId login(const std::string &pUsername,
        const std::string &pPassword);

    virtual bool logout(const greenhills::core::CClientId &pClientId);

    virtual greenhills::core::CRoomId createRoom(
        const greenhills::core::CClientId &pClientId,
        const std::string &pRoomName,
        const greenhills::core::CRoomType &pRoomType,
        const std::string &pPassword);
    virtual bool joinRoom(const greenhills::core::CClientId &pClientId,
        const greenhills::core::CRoomId &pRoomId, const std::string &pPassword);
    virtual bool leaveRoom(const greenhills::core::CClientId &pClientId,
        const greenhills::core::CRoomId &CRoomId);

    virtual greenhills::core::CItemId addItem(
        const greenhills::core::CClientId &pClientId,
        const greenhills::core::CRoomId &pRoomId, const std::string &pItemName,
        const greenhills::core::CItemType &pItemType);
    virtual bool removeItem(const greenhills::core::CClientId &pClientId,
        const greenhills::core::CRoomId &pRoomId,
        const greenhills::core::CItemId &pItemId);
    virtual bool updateItem(const greenhills::core::CClientId &pClientId,
        const greenhills::core::CRoomId &pRoomId,
        const greenhills::core::CItemId &pItemId,
        const unsigned char *pData, unsigned int pLength);

private Q_SLOTS:
    void onTimeout();
    void onEventReceived(const greenhills::event::CEvent &pEvent);

private:
    net::CMsgClientSocketConnection *mControlConnection;
    net::CMsgClientSocketConnection *mEventConnection;

    greenhills::event::CSynchronousChannel *mControlChannel;
    greenhills::event::CAsynchronousChannel *mEventChannel;
};

} //namespace core
} //namespace client
} //namespace greenhills
